﻿using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Security.Claims;
using System.Web;
using Yes.Application.User;
using Yes.Application.User.Dto;
using Yes.Core.Extensions;
using Yes.Core.Helper;
using Yes.Domain.Context;
using Yes.Web.Blog.Attribute;

namespace Yes.Web.Blog.Areas.Admin.Controllers
{
    
    [Area("Admin")]
    public class HomeController : BaseController
    {
        private IUserService _userService;
        private IUserContext _userContext;

        public HomeController(IUserService userService,
            IUserContext userContext)
        {
            _userService = userService;
            _userContext = userContext;
        }

        
        public ActionResult Index()
        {
            return View();
        }

        [AllowAnonymous,HttpGet]
        public ActionResult Login()
        {
            return View();
        }

        [AllowAnonymous, HttpPost]
        public ActionResult Login(UserLoginDto dto)
        {
            return Json(_userService.Login(dto));
        }

        [HttpGet]
        public ActionResult Logout()
        {
            _userService.Logout();
            return RedirectToAction("Login");
        }

        [HttpGet]
        public ActionResult ChangePasword()
        {
            return View();
        }


        [HttpPost]
        public ActionResult ChangePasword(PasswordEditDto dto)
        {
            var loginInfo = _userContext.CurrentUser;
            return Json(_userService.ChangePasword(dto, loginInfo.MapTo<LoginInfo>()));
        }

    }
}